// 
//  Copyright 2011  comphilip
// 
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
// 
//        http://www.apache.org/licenses/LICENSE-2.0
// 
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using System.Globalization;

namespace Dict4Engkoo.Engkoo {
	internal class Collocation {
		private JToken _token;
		
		public Collocation(JToken Token){
			_token = Token;
		}
		
		public string Relation {
			get {
				return _token["$REL"].Value<string>();	
			}
		}
		public IEnumerable<string> Collocations {
			get {
				JToken colls = _token["C"];
				if (colls == null)
					yield break;
				JArray collArray = colls as JArray;
				if (collArray != null)
					foreach(var item in collArray)
						yield return GetCollocationFromToken(item);
				else
					yield return GetCollocationFromToken(colls);
			}
		}
		
		private string GetCollocationFromToken(JToken Token){
			JToken t1 = Token["$T1"];
			JToken t2 = Token["$T2"];
			return string.Format(CultureInfo.InvariantCulture, "{0} {1}", 
				t1 != null ? Uri.UnescapeDataString(t1.Value<string>()) : string.Empty , t2 != null ? Uri.UnescapeDataString(t2.Value<string>()) : string.Empty);
		}
	}
}

